package com.huping.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huping.mapper.*;
import com.huping.pojo.*;
import com.huping.service.CourseService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    CourseMapper courseMapper;
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    ScMapper scMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    TcMapper tcMapper;

    @Override
    public PageBean courseSelect(Claims claims, JSONObject param) {

        String userid= (String) claims.get("userid");
        String kind = (String)claims.get("kind");
        String courseId = param.getString("courseId");
        String name = param.getString("name");
        String teacher = param.getString("teacher");

        if(kind.equals("2")){
            PageHelper.startPage(Integer.parseInt(param.getString("page")),Integer.parseInt(param.getString("size")));
            List<Course> r = courseMapper.teacherCourseSelect(userid,courseId,name);
            Page<Course> p = (Page<Course>) r;
            PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
            return pageBean;
        }
        else {
            PageHelper.startPage(Integer.parseInt(param.getString("page")),Integer.parseInt(param.getString("size")));
            List<Map<String ,Object>> r = courseMapper.studentCourseSelect(userid,courseId,name,teacher);
//            List<Map<String,Object>> r = courseMapper.studentCourseSelect(userid,courseId,name,teacher);
            Page<Map<String ,Object>> p = (Page<Map<String ,Object>>) r;
            PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
            return pageBean;
        }

    }

    @Override
    public Result create(JSONObject param, Claims claims) {
        String courseId = param.getString("courseId");

        String name = param.getString("name");
        String profile = param.getString("profile");
        String userid = (String)claims.get("userid");
        String teacher = teacherMapper.getTeacherInformationByUserid(userid).getName();

        if(courseMapper.getCourseNumByCourseId(courseId)>=1){
            return Result.error("课程号已存在");
        }
        else {
            int rCourse = courseMapper.courseInsert(courseId,name,profile);
            int rTc = tcMapper.tcInsert(userid,courseId);
            if(rCourse!=0 && rTc!=0) return Result.success("创建课程成功");
            else return Result.error("添加课程失败");
        }

    }

    @Override
    public Result join(JSONObject param, Claims claims) {
        String userid = (String) claims.get("userid");
        String courseId = param.getString("courseId");
        String teacherId = param.getString("teacherId");
        if(tcMapper.checkCourseExist(courseId,teacherId)!=1){
            return Result.error("课程不存在");
        }
        else {
            int r = scMapper.ucInsert(userid,courseId,teacherId);
            if(r==1) return Result.success("加入课程成功");
            else return Result.error("加入失败");
        }

    }

    @Override
    public Result detail(JSONObject param, Claims claims) {
        String userid= (String) claims.get("userid");
        String kind= (String) claims.get("kind");
        String courseId = param.getString("courseId");
        if (kind.equals("2")){
            Map<String,Object> courseResult = courseMapper.getCourseByCourseId(courseId);
            String teacherName = teacherMapper.getTeacherInformationByUserid(userid).getName();
            courseResult.put("teacherName",teacherName);
            return Result.success(courseResult);
        }
        else{
            Map<String,Object> courseResult = courseMapper.getCourseByCourseId(courseId);
            String teacherName = teacherMapper.courseDetailsStudentGetTeacherName(userid,courseId).getName();
            courseResult.put("teacherName",teacherName);
            return Result.success(courseResult);
        }


    }

    @Override
    public PageBean students(JSONObject param, Claims claims) {
        String courseId = param.getString("courseId");
        String userid = (String) claims.get("userid");
        PageHelper.startPage(Integer.parseInt(param.getString("page")),Integer.parseInt(param.getString("size")));
        List<Student> r = studentMapper.getStudentsByCourseId(courseId,userid);
        Page<Student> p = (Page<Student>) r;

        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());


        return pageBean;
    }

    @Override
    public Result modify(JSONObject param) {
        String courseId = param.getString("courseId");
        String courseName = param.getString("courseName");
        String profile = param.getString("profile");
        int r =courseMapper.courseUpdate(courseId,courseName,profile);
        if(r==1) return Result.success("修改成功");
        else return Result.error("修改失败");
    }


}
